package com.magima.core.config;

import android.webkit.URLUtil;

import org.apache.http.client.utils.URIUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;

public class GlobalConfig {
	public static final int MODE_HTTP = 1;
	public static final int MODE_HTTPS = 2;
	
	// config
	private static JSONObject apiConfig = null;
	
	public static void setGlobalConfig(JSONObject obj){
		apiConfig = obj;
	}
	
	public static JSONObject getGlobalConfig(){
		return apiConfig;
	}
	
	public static String getConfig(String key) {
		try {
			if ( apiConfig == null )
				return null;
			
			return apiConfig.getString(key);
		} catch (JSONException e) {
			return null;
		}
	}

    public static String[] getConfigArray(String key){
        try {
            if ( apiConfig == null )
                return null;

            JSONArray array = apiConfig.getJSONArray(key);
            String[] configs = new String[array.length()];
            for ( int i =0; i < array.length(); i++) {
                configs[i] = array.getString(i);
            }
            return configs;
        } catch (JSONException e) {
            return null;
        }
    }
	
	public synchronized static void setMode(int mode){
		if ( apiConfig == null )
			return;
		
		if(mode != MODE_HTTP && mode != MODE_HTTPS)
			return;
		
		Iterator<String> keys = apiConfig.keys();
		while(keys.hasNext()){
			String key = keys.next();
			String value = getConfig(key);
			
			if(value != null){
				if(mode == MODE_HTTP){
					if(value.startsWith("https")){
						value = value.replace("https", "http");
					}
				}else if(mode == MODE_HTTPS){
					if(value.startsWith("http") && !value.startsWith("https")){
						value = value.replace("http", "https");
					}
				}
			}
			
			try {
				apiConfig.put(key, value);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

    public synchronized static void changeHost(String host){
        if ( apiConfig == null )
            return;

        Iterator<String> keys = apiConfig.keys();
        while(keys.hasNext()){
            String key = keys.next();
            String value = getConfig(key);
            if ( !key.endsWith("Api") )
                continue;

            if(value != null){
                URL uri;
                try {
                    if ( URLUtil.isValidUrl(value) ) {
                        uri = new URL(value);

                        URI ret = URIUtils.createURI(uri.getProtocol(), host, uri.getPort(), uri.getPath(), uri.getQuery(), null);
                        value = ret.toString();
                    } else {
                        continue;
                    }
                } catch (MalformedURLException e) {
                    continue;
                } catch (URISyntaxException e) {
                    continue;
                }
            }

            try {
                apiConfig.put(key, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
